const errors = require('../../errors');
const Validate = require('../../util/baseUtil/Validate');
const LoginUtil = require('../../util/businessUtil/LoginUtil');
const crypto = require('crypto')
const LogUtil = require('../../util/baseUtil/LogUtil')
const moment = require('moment')
const AdminUser = require('../../model/AdminUser')
const Role = require('../../model/Role')
const MyPrivateAccount = require('../../model/MyPrivateAccount')

module.exports = {

    //用户CRUD

    getOneAdmin: async(req, res, next) => {
        let data = await AdminUser.findOne({ _id: req.body.id, isDelete: 0 }).populate('role store').exec()
        res.ok('获取成功', data)
    },

    deleteOneAdmin: async(req, res, next) => {
        let data = await AdminUser.findOne({ _id: req.body.id, isDelete: 0 }).exec()
        data.isDelete = 1
        await data.save()
        res.ok('删除成功')
    },

    getAdminList: async(req, res, next) => {
        let options = {}
        options.isDelete = 0
        if (req.body.account) {
            options.account = new RegExp(req.body.account, 'i')
        }
        if (req.body.role) {
            options.role = req.body.role
        }
        let count = await AdminUser.countDocuments(options).exec()
        let page = req.body.page || 1
        let pageSize = req.body.pageSize || count
        let data = await AdminUser.find(options).skip((page - 1) * pageSize).limit(pageSize).populate('role').exec()
        let result = {}
        result.total = count
        result.currentPage = page
        result.data = data
        result.pageSize = pageSize
        res.ok('获取成功', result)
    },

    changeOneAdmin: async(req, res, next) => {
        let md5 = crypto.createHash('md5');
        if (req.body.password) {
            var verified = md5.update(req.body.password).digest('hex');
        }
        if (req.body.id) {
            let adminuser = await AdminUser.findOne({ _id: req.body.id }).exec()
            adminuser.role = req.body.role
            if (verified) {
                adminuser.password = verified
            }
            adminuser.store = req.body.store
            await adminuser.save()
            res.ok('修改成功')
        } else {
            let newAdminUser = new AdminUser({
                account: req.body.account,
                password: verified,
                role: req.body.role,
                store: req.body.store,
                isDelete: 0
            })
            await newAdminUser.save()
            res.ok('创建成功')
        }
    },
    

    getRoles: async(req, res, next) => {
        let role = await Role.find({ isDelete: 0 }, 'name').exec()
        res.ok('获取权限成功', role)
    },

    addAccount: async(req, res, next) => {
        try {
            let newAccount = MyPrivateAccount({
                name:req.body.name,
                uid:req.body.uid,
                updatetime: new Date(),
                isDelete: 0,
            })
            await newAccount.save()
            return res.ok('添加成功')
        } catch (e) {
            next(e)
        }
    },


}